package com.huidz.calendar.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.huidz.calendar.R;


public class FuWuTiaoKuanActivity extends Activity implements View.OnClickListener {
    private ImageView comeback;
    private WebView fwtkweb;
    private ProgressBar progressBar;
private TextView location_text;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fuwutiaokuan);
        comeback = findViewById(R.id.fwtktitle_comeback);
        comeback.setOnClickListener(this);
            init();
    }


    /**
     * 初始加载
     */
    public void init() {
        Intent tof=getIntent();
        location_text=findViewById(R.id.location_text);
        progressBar = findViewById(R.id.progressbar);//进度条
        fwtkweb = findViewById(R.id.fwtkweb);
        fwtkweb.loadUrl(tof.getStringExtra("url"));
        location_text.setText(tof.getStringExtra("title"));
        fwtkweb.setWebChromeClient(webChromeClient);
        fwtkweb.setWebViewClient(webViewClient);
        WebSettings webSettings=fwtkweb.getSettings();
        webSettings.setJavaScriptEnabled(true);
    }

    //WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等
    private WebChromeClient webChromeClient=new WebChromeClient(){
        //不支持js的alert弹窗，需要自己监听然后通过dialog弹窗
        @Override
        public boolean onJsAlert(WebView webView, String url, String message, JsResult result) {
            AlertDialog.Builder localBuilder = new AlertDialog.Builder(webView.getContext());
            localBuilder.setMessage(message).setPositiveButton("确定",null);
            localBuilder.setCancelable(false);
            localBuilder.create().show();
            result.confirm();
            return true;
        }

        //获取网页标题
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }

        //加载进度回调
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            progressBar.setProgress(newProgress);
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (fwtkweb.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK){//点击返回按钮的时候判断有没有上一页
            fwtkweb.goBack(); // goBack()表示返回webView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

    private WebViewClient webViewClient=new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {//页面加载完成
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {//页面开始加载
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.equals("http://www.google.com/")){
                Toast.makeText(FuWuTiaoKuanActivity.this,"国内不能访问google,拦截该url",Toast.LENGTH_LONG).show();
                return true;//表示我已经处理过了
            }
            return super.shouldOverrideUrlLoading(view, url);
        }

    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //释放资源
        fwtkweb.destroy();
        fwtkweb=null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fwtktitle_comeback:
                onBackPressed();
                break;
        }
    }

}
